package chapter4.No4_1_使用reentrantLock类.demo6;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * !
 *
 * @author liujian
 * @version 2016/11/21 0021
 */
public class ServiceClass {
    ReentrantLock lock = new ReentrantLock();
    Condition condition = lock.newCondition();

    public void methodA() {
        lock.lock();
        System.out.println("A方法执行开始");
        try {
            Thread.sleep(1000);
            condition.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("A方法执行开始");
        lock.unlock();
    }

    public void methodB() {
        lock.lock();
        System.out.println("B方法执行开始");
        try {
            Thread.sleep(1000);
            condition.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("B方法执行开始");
        lock.unlock();
    }

    public void methodC() {
        lock.lock();
        System.out.println("C方法执行开始");
        try {
            Thread.sleep(1000);
            condition.signalAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("C方法执行开始");
        lock.unlock();
    }
}
